<template>
  <div class="package-basic">
    <el-form ref="form" :model="assetsForm" :rules="rules" label-width="220px">
      <el-form-item label="资产包名称" prop="name">
        <el-input v-model="assetsForm.name"/>
      </el-form-item>
      <el-form-item label="是否免费">
        <el-radio-group v-model="assetsForm.beFree">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="定价" prop="originPrice">
        <el-input v-model="assetsForm.originPrice" placeholder="0.00"/>
      </el-form-item>
      <el-form-item label="优惠价" prop="discountPrice">
        <el-input v-model="assetsForm.discountPrice" placeholder="0.00"/>
      </el-form-item>
      <el-form-item label="资产包有效期">
        <el-radio-group v-model="assetsForm.validityTimeType">
          <el-radio label="days">固定天数</el-radio>
          <el-radio label="times">固定日期</el-radio>
          <el-radio label="forever">无限期</el-radio>
        </el-radio-group>
        <p v-if="assetsForm.validityTimeType === 'days'" style="margin:0; color:#8a8a8a;margin-top:10px;">
          有效天数
          <el-input-number v-model="assetsForm.days" :min="1" :max="100" style="margin-left:10px;"/>
        </p>
        <p v-if="assetsForm.validityTimeType === 'times'" style="margin:0; color:#8a8a8a;margin-top:10px;">
          有效日期
          <el-date-picker
            v-model="assetsForm.daterange"
            type="daterange"
            range-separator="|"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            style="margin-left:10px;"
          />
        </p>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="nextStep">保存并继续</el-button>
        <el-button @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  components: {
  },
  props: {
    assets: {
      type: Object,
      default() {
        return { }
      }
    }
  },
  data() {
    const originPrice = (rule, value, callback) => {
      console.log(typeof value)
      if (/^\+?[0-9]*$/.test(value) === false) {
        callback(new Error('请填入数字'))
      }
    }
    const discountPrice = (rule, value, callback) => {
      if (/^\+?[0-9]*$/.test(value) === false) {
        callback(new Error('请填入有效数字'))
      } else {
        if (value > this.assetsForm.originPrice) {
          callback(new Error('不能多于定价'))
        } else {
          callback()
        }
      }
    }
    return {
      rules: {
        name: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        originPrice: [
          { required: true, message: '必填项', trigger: 'blur' },
          { validator: originPrice, trigger: 'blur' }
        ],
        discountPrice: [
          { required: true, message: '必填项', trigger: 'blur' },
          { validator: discountPrice, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    assetsForm() {
      console.log(this.assets)
      return this.assets
    }
  },
  methods: {
    nextStep() {
      if (!this.assetsForm.id && this.assetsForm.daterange) {
        this.assetsForm.startTime = this.assetsForm.daterange[0].getTime()
        this.assetsForm.endTime = this.assetsForm.daterange[1].getTime()
      }
      this.$emit('goNext', this.assetsForm)
    },
    handleCancel() {
      this.$router.go(-1)
    }
  }
}
</script>

<style lang="scss" scoped>
.package-basic {
  .el-form {
    margin-top: 30px;
    .el-input {
      width: 200px;
    }
    .el-select {
      width: 130px;
    }
  }
}
</style>
